<?php
class CommentAction extends CommonAction {
    public function __construct(){
		parent::__construct();
		
		$this->modelObject = D('Comment');
		$this->modelViewObject = D('CommentView');
    }
   
    public function lists(){
		$thisPage = I('p', 1);
		$map = $result = array();
		
		$map['target_id'] = I('id', 0, 'intval');
		$map['types']     = I('types', '', 'strval');
		$map['status'] =1;
		
		//获得列表
		$list = $this->modelViewObject->getPageList($map, $thisPage);
		foreach($list as $key=>$val){
			$list[$key]['username'] = formateUsername($val['username']);
			$list[$key]['format_time'] = dateLocalFormat($val['add_time']);
		}
		//分页
		$page = $this->modelViewObject->buildPage($map, $thisPage);
		if($list){
			$result['error'] = 1;
			$result['page'] = $page;
			$result['list'] = $list;
		}else{
			$result['error'] = 0;
		}
		$this->ajaxReturn($result);
    }
   
    /**
	* 评论动作
	* @param $types goods : 游戏, news: 资讯
	*/
	public function post(){
		//echo $id;
		$id    = I('id', 0, 'intval');
		$types = I('types', 'websites');
		$star  = I('star', 5);
		if($id == 0 && !in_array($types, C('SHARE_ACTION'))){
			$result['error']   = 0;
			$result['message'] = '参数错误';
			$this->ajaxReturn($result);
		}

		$data = array();
		$data['target_id'] = $id;
		$data['name']      = I('name', '', 'trim');
		$data['parent_id'] = I('parent_id', 0, 'intval');
		$data['add_time']  = NOW_TIME;
		$data['types']     = $types;
		$data['star_count']= $star;
		$data['content']   = I('content', '', 'strip_tags');
		$data['member_id'] = $this->memberInfo['id'];
		$data['ip']        = get_client_ip(); //1已经审核
		$data['status']    = 1; //1已经审核
		
		if(empty($data['name'])){
			$result['error']   = 0;
			$result['message'] = '评论人不能为空!';
			$this->ajaxReturn($result);		
		}
		if(!empty($data['content'])){//内容不为空的时候为评论，内容为空的时候为用户的行为记录
			//判断是否有敏感词
			/*if(!($filterValue = F('filterValue'))){
				$filterValue = M('Config')->where('id=1')->getField('filter_value');
				$filterValue = explode(',', $filterValue);
				F('filterValue', $filterValue);
			}
			if($filterValue){
				foreach($filterValue as $fv){
					if(substr_count($data['content'], $fv)){
						$data['status'] = 2;//需要审核
						$msg = '评论成功,但是有敏感词语，等待管理员审核后即可显示!';
						break;
					}
				}
			}*/

			if($this->modelObject->add($data)){
				$result['error']   = 1;
				$result['message'] = '评论成功!';
				$this->ajaxReturn($result);
			}else{
				$result['error']   = 0;
				$result['message'] = '评论失败!';
				$this->ajaxReturn($result);	
			}
		}
	}
}//end class